package com.dingreading.cloud.ssxcx.service.impl;

import com.dingreading.cloud.common.util.MstXcxClassType;
import com.dingreading.cloud.ssxcx.dto.XcxLessonMaterialsDto;
import com.dingreading.cloud.ssxcx.entity.XcxLessonMaterials;
import com.dingreading.cloud.ssxcx.entity.table.XcxLessonMaterialsTableDef;
import com.dingreading.cloud.ssxcx.mapper.XcxLessonMaterialsMapper;
import com.dingreading.cloud.ssxcx.service.XcxLessonMaterialsService;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryWrapper;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 小程序课节资料表 服务层实现。
 *
 * @author EDY
 * @since 2025-08-15
 */
@Service
public class XcxLessonMaterialsServiceImpl extends BaseServiceImpl<XcxLessonMaterialsMapper, XcxLessonMaterials, XcxLessonMaterialsTableDef> implements XcxLessonMaterialsService {

    @Override
    protected XcxLessonMaterialsTableDef getTable() {
        return XcxLessonMaterialsTableDef.xcxLessonMaterials;
    }

    @Override
    public List<XcxLessonMaterialsDto> listByLessonUidAndStage(String subjectLessonUid, Integer materialStage) {
        QueryCondition condition = QueryCondition.createEmpty()
                .and(table.subjectLessonUid.eq(subjectLessonUid))
                .and(table.materialStage.eq(materialStage))
                .and(table.enabled.eq(1));

        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.id.asc());

        List<XcxLessonMaterialsDto> xcxLessonMaterialsDtoList = getMapper().selectListWithRelationsByQueryAs(wrapper, XcxLessonMaterialsDto.class);
        return xcxLessonMaterialsDtoList;
    }

    @Override
    public List<XcxLessonMaterialsDto> getMistakeQuestions(String subjectLessonUid, List<String> questionUids) {
        QueryCondition condition = QueryCondition.createEmpty()
                .and(table.subjectLessonUid.eq(subjectLessonUid))
                .and(table.uid.in(questionUids))
                .and(table.materialStage.eq(MstXcxClassType.INCLASS.getType()))
                .and(table.enabled.eq(1));

        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.id.asc());

        List<XcxLessonMaterialsDto> xcxLessonMaterialsDtoList = getMapper().selectListWithRelationsByQueryAs(wrapper, XcxLessonMaterialsDto.class);
        return xcxLessonMaterialsDtoList;
    }

    @Override
    public XcxLessonMaterials getLatest(String subjectLessonUid, Integer materialStage) {
        QueryCondition condition = QueryCondition.createEmpty()
                .and(table.subjectLessonUid.eq(subjectLessonUid))
                .and(table.projectUid.isNotNull())
                .and(table.materialStage.eq(materialStage))
                .and(table.enabled.eq(1));

        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.id.desc())
                .limit(1);
        return getMapper().selectOneByQuery(wrapper);
    }
}
